package com.atguigu.upp.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * Created by Smexy on 2022/12/2
 */
public interface CKMapper
{
    //建表语句
    @Update("create table ${table}(uid String, ${tagColumn}) engine=MergeTree order by uid ")
    void createWideTable(@Param("table") String tableName,@Param("tagColumn") String tagColumn);

    //手动实现幂等性，为了保证每天多次执行宽表导入，幂等。在导入之前，先删除之前已经写入的表
    @Update("drop table if exists ${table}")
    void dropTable(@Param("table") String tableName);


    /*
        初步分群
insert into user_tag_value_decimal
select
    col1.1 tag_code,if(col1.2 = '',0,col1.2) tag_value ,groupBitmapState(toUInt64(uid)) , '2020-06-14'
from
    (
        select
            uid,
            arrayJoin(
                    [
                        ('tag_consumption_behavior_order_7damount', tag_consumption_behavior_order_7damount)
                        ]
                )  col1
        from up_tag_merge_2020_06_14
        where length(uid) > 0
        )tmp
group by col1.1,col1.2;
     */
    @Insert("insert into ${table} select col1.1 tag_code,if(col1.2 = '','0',col1.2) tag_value ,groupBitmapState(toUInt64(uid)) , #{doDate}" +
        "  from (  select   uid,  arrayJoin( [ ${tagSql} ] ) col1   from ${source}  where length(uid) > 0   ) tmp group by col1.1,col1.2" +
        "")

    void insertBitmap(@Param("table") String tableName,@Param("doDate") String doDate,@Param("source") String sourceTable,
                      @Param("tagSql")String tagSql);

    //保证写入bitmap的幂等性
    @Update("alter table ${table} delete where dt =#{doDate};")
    void cleanHistoryBitmap(@Param("table") String tableName,@Param("doDate") String doDate);
}
